home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / wwbbs31_source.lha / WWBBS / EditorSrc / editor.c < prev    next >
C/C++ Source or Header  |  1994-10-15  |  8KB  |  347 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10.  
  11. #include "editor_rev.h"
  12.  
  13. char *ver=VERSTAG;
  14.  
  15. struct TextNode {
  16.     struct Node tn_Node;
  17.     char tn_Buffer[80];
  18. };
  19.  
  20. BOOL ask(BYTE *,BOOL);
  21.  
  22. BOOL panic=FALSE;
  23.  
  24. void __regargs _CXBRK(void);
  25.  
  26. void __regargs _CXBRK(void)
  27.     {
  28.         panic=TRUE;
  29.     }
  30.  
  31. void main(int argc,char **argv)
  32.     {
  33.         struct List TextList;
  34.         struct TextNode *tnode=NULL;
  35.         int x=0,y=0,abort=FALSE;
  36.         if(argc!=2)
  37.             exit(RETURN_FAIL);
  38.         NewList(&TextList);
  39.         printf("\n~hWorld Wide BBS Editor - Press CONTROL-P for Help\n~i");
  40.         {
  41.             BPTR fh;
  42.             if(fh=Open(argv[1],MODE_OLDFILE))
  43.                 {
  44.                     BYTE buff[256];
  45.                     while(FGets(fh,buff,255))
  46.                         {
  47.                             {
  48.                                 char *p;
  49.                                 if(p=strchr(buff,'\n'))
  50.                                     *p=NULL;
  51.                             }
  52.                             if(tnode=AllocMem(sizeof(struct TextNode),MEMF_CLEAR))
  53.                                 {
  54.                                     if(strlen(buff)>79)
  55.                                         {
  56.                                             strncpy(tnode->tn_Buffer,buff,79);
  57.                                             tnode->tn_Buffer[79]=NULL;
  58.                                         }
  59.                                     else
  60.                                         strcpy(tnode->tn_Buffer,buff);
  61.                                     printf("%s\n",tnode->tn_Buffer);
  62.                                     AddTail(&TextList,(struct Node *) tnode);
  63.                                     y++;
  64.                                 }
  65.                         }
  66.                     Close(fh);
  67.                     DeleteFile(argv[1]);
  68.                 }
  69.         }
  70.         {
  71.             if(tnode=AllocMem(sizeof(struct TextNode),MEMF_CLEAR))
  72.                 {
  73.                     AddTail(&TextList,(struct Node *) tnode);
  74.                     printf("~p%d:\n~i",y+1);
  75.                 }
  76.         }
  77.         {
  78.             int c;
  79.             int kg=TRUE;
  80.             while(!panic && kg)
  81.                 {
  82.                     c=getch();
  83.                     if(c==EOF)
  84.                         break;
  85.                     {
  86.                         int printprompt=FALSE;
  87.                         switch(c)
  88.                             {
  89.                                 case 1: /* Abort */
  90.                                     if(x)
  91.                                         printf("\n");
  92.                                     if(ask("Abort Message",FALSE))
  93.                                         {
  94.                                             abort=TRUE;
  95.                                             kg=FALSE;
  96.                                         }
  97.                                     else
  98.                                         {
  99.                                             printf("\n~i");
  100.                                             printprompt=TRUE;
  101.                                         }
  102.                                     break;
  103.                                 case 12: /* List */
  104.                                     if(y)
  105.                                         {
  106.                                             if(x)
  107.                                                 printf("\n");
  108.                                             printf("~o\n");
  109.                                             {
  110.                                                 struct TextNode *wn;
  111.                                                 for(wn=(struct TextNode *) TextList.lh_Head;wn->tn_Node.ln_Succ;wn=(struct TextNode *) wn->tn_Node.ln_Succ)
  112.                                                     {
  113.                                                         if(wn!=tnode)
  114.                                                             printf("%s\n",wn->tn_Buffer);
  115.                                                     }
  116.                                             }
  117.                                             printf("\n~i");
  118.                                             printprompt=TRUE;
  119.                                         }
  120.                                     break;
  121.                                 case 14: /* New */
  122.                                     if(x)
  123.                                         printf("\n");
  124.                                     if(ask("Clear Text",FALSE))
  125.                                         {
  126.                                             struct TextNode *wn,*nn;
  127.                                             wn=(struct TextNode *) TextList.lh_Head;
  128.                                             while(nn=(struct TextNode *) wn->tn_Node.ln_Succ)
  129.                                                 {
  130.                                                     if(wn!=tnode)
  131.                                                         {
  132.                                                             Remove((struct Node *) wn);
  133.                                                             FreeMem(wn,sizeof(struct TextNode));
  134.                                                         }
  135.                                                     else
  136.                                                         strcpy(wn->tn_Buffer,"");
  137.                                                     wn=nn;
  138.                                                 }
  139.                                             x=y=0;
  140.                                             printf("~s\nMessage text cleared.\n\n~i");
  141.                                             printprompt=TRUE;
  142.                                         }
  143.                                     else
  144.                                         {
  145.                                             printf("\n~i");
  146.                                             printprompt=TRUE;
  147.                                         }
  148.                                     break;
  149.                                 case 16: /* Help */
  150.                                     if(x)
  151.                                         printf("\n");
  152.                                     printf("~o\n");
  153.                                     printf("[CONTROL-A] Abort Message\n");
  154.                                     printf("[CONTROL-L] List Message\n");
  155.                                     printf("[CONTROL-N] New, Clear Text\n");
  156.                                     printf("[CONTROL-P] Help\n");
  157.                                     printf("[CONTROL-Z] Save Message\n");
  158.                                     printf("\n~i");
  159.                                     printprompt=TRUE;
  160.                                     break;
  161.                                 case 26: /* Save */
  162.                                     if(x)
  163.                                         printf("\n");
  164.                                         if(!x)
  165.                                             {
  166.                                                 Remove((struct Node *) tnode);
  167.                                                 FreeMem(tnode,sizeof(struct TextNode));
  168.                                                 tnode=NULL;
  169.                                             }
  170.                                         kg=FALSE;
  171.                                     break;
  172.                                 case '\r':
  173.                                     if(tnode=AllocMem(sizeof(struct TextNode),MEMF_CLEAR))
  174.                                         {
  175.                                             AddTail(&TextList,(struct Node *) tnode);
  176.                                             x=0;
  177.                                             y++;
  178.                                             printf("\n");
  179.                                         }
  180.                                     break;
  181.                                 case '\b':
  182.                                     if(tnode)
  183.                                         {
  184.                                             if(x)
  185.                                                 {
  186.                                                     x--;
  187.                                                     tnode->tn_Buffer[x]=NULL;
  188.                                                     printf("\b \b");
  189.                                                 }
  190.                                             else
  191.                                                 {
  192.                                                     if(y)
  193.                                                         {
  194.                                                             struct TextNode *wn;
  195.                                                             if(wn=(struct TextNode *) tnode->tn_Node.ln_Pred)
  196.                                                                 {
  197.                                                                     Remove((struct Node *) tnode);
  198.                                                                     FreeMem(tnode,sizeof(struct TextNode));
  199.                                                                     tnode=wn;
  200.                                                                     x=strlen(tnode->tn_Buffer);
  201.                                                                     y--;
  202.                                                                     printprompt=TRUE;
  203.                                                                 }
  204.                                                         }
  205.                                                 }
  206.                                         }
  207.                                     break;
  208.                                 case '\t':
  209.                                     if(tnode)
  210.                                         {
  211.                                             if(x<75)
  212.                                                 {
  213.                                                     int i,t;
  214.                                                     t=5-(x%5);
  215.                                                     for(i=0;i<t;i++)
  216.                                                         {
  217.                                                             strcat(tnode->tn_Buffer," ");
  218.                                                             printf(" ");
  219.                                                             x++;
  220.                                                         }
  221.                                                 }
  222.                                         }
  223.                                     break;
  224.                                 default:
  225.                                     if(tnode)
  226.                                         {
  227.                                             if(isprint(c))
  228.                                                 {
  229.                                                     if(x<79)
  230.                                                         {
  231.                                                             sprintf(&tnode->tn_Buffer[strlen(tnode->tn_Buffer)],"%c",c);
  232.                                                             printf("%c",c);
  233.                                                             x++;
  234.                                                         }
  235.                                                     else
  236.                                                         {
  237.                                                             if(strchr(tnode->tn_Buffer,' '))
  238.                                                                 {
  239.                                                                     char *p;
  240.                                                                     p=&tnode->tn_Buffer[strlen(tnode->tn_Buffer)-1];
  241.                                                                     while(!isspace(*p))
  242.                                                                         {
  243.                                                                             printf("\b \b");
  244.                                                                             p--;
  245.                                                                         }
  246.                                                                     *p=NULL;
  247.                                                                     p++;
  248.                                                                     if(tnode=AllocMem(sizeof(struct TextNode),MEMF_CLEAR))
  249.                                                                         {
  250.                                                                             AddTail(&TextList,(struct Node *) tnode);
  251.                                                                             strcpy(tnode->tn_Buffer,p);
  252.                                                                             sprintf(&tnode->tn_Buffer[strlen(tnode->tn_Buffer)],"%c",c);
  253.                                                                             x=strlen(tnode->tn_Buffer);
  254.                                                                             y++;
  255.                                                                             printf("\n");
  256.                                                                             printf(tnode->tn_Buffer);
  257.                                                                         }
  258.                                                                 }
  259.                                                             else
  260.                                                                 {
  261.                                                                     if(tnode=AllocMem(sizeof(struct TextNode),MEMF_CLEAR))
  262.                                                                         {
  263.                                                                             AddTail(&TextList,(struct Node *) tnode);
  264.                                                                             tnode->tn_Buffer[0]=c;
  265.                                                                             tnode->tn_Buffer[1]=NULL;
  266.                                                                             x=1;
  267.                                                                             y++;
  268.                                                                             printf("\n");
  269.                                                                             printf(tnode->tn_Buffer);
  270.                                                                         }
  271.                                                                 }
  272.                                                         }
  273.                                                 }
  274.                                         }
  275.                                     break;
  276.                             }
  277.                         if(printprompt)
  278.                             {
  279.                                 printf("~p%d:\n~i",y+1);
  280.                                 if(x && tnode)
  281.                                     printf(tnode->tn_Buffer);
  282.                             }
  283.                     }
  284.                 }
  285.         }
  286.         if(!abort)
  287.             {
  288.                 BPTR fh;
  289.                 if(fh=Open(argv[1],MODE_NEWFILE))
  290.                     {
  291.                         for(tnode=(struct TextNode *) TextList.lh_Head;tnode->tn_Node.ln_Succ;tnode=(struct TextNode *) tnode->tn_Node.ln_Succ)
  292.                             {
  293.                                 FPuts(fh,tnode->tn_Buffer);
  294.                                 FPutC(fh,'\n');
  295.                             }
  296.                         Close(fh);
  297.                     }
  298.             }
  299.         {
  300.             struct TextNode *wn,*nn;
  301.             wn=(struct TextNode *) TextList.lh_Head;
  302.             while(nn=(struct TextNode *) wn->tn_Node.ln_Succ)
  303.                 {
  304.                     FreeMem(wn,sizeof(struct TextNode));
  305.                     wn=nn;
  306.                 }
  307.         }
  308.         exit(0);
  309.     }
  310.  
  311. BOOL ask(BYTE *prompt,BOOL def)
  312.     {
  313.         int c;
  314.         BOOL ret=NULL;
  315.         int kg=TRUE;
  316.         if(prompt)
  317.             printf("~p\n%s (%s,%s)? ",prompt,(def) ? "[Y]" : "Y",(!def) ? "[N]" : "N");
  318.         while(!panic && kg)
  319.             {
  320.                 c=getch();
  321.                 if(c==EOF)
  322.                     break;
  323.                 {
  324.                     switch(c)
  325.                         {
  326.                             case '\r':
  327.                                 ret=def;
  328.                                 kg=FALSE;
  329.                                 break;
  330.                             case 'y':
  331.                             case 'Y':
  332.                                 ret=TRUE;
  333.                                 kg=FALSE;
  334.                                 break;
  335.                             case 'n':
  336.                             case 'N':
  337.                                 ret=FALSE;
  338.                                 kg=FALSE;
  339.                                 break;
  340.                         }
  341.                 }
  342.             }
  343.         if(!kg)
  344.             printf((ret) ? "Yes\n" : "No\n");
  345.         return(ret);
  346.     }
  347.